package rauguelu.feria.tapas;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import rauguelu.items.Tapa;
import rauguelu.programaEventos.OverlayMapa;
import rauguelu.programaEventos.R;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class BarDetalle extends ListActivity{

	Long idBar;
	BaseDatosFeriasHelper miBBDDHelper;
	Cursor bar;
	List<Tapa> tapas;
	String rutaImagen;
	String urlWeb;
	String nombreBar;

	
	Typeface font;
	Typeface font2;
	  
	@Override
	  public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        setContentView(R.layout.bar_detalle);
	        
	        font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
		  	font2 = Typeface.createFromAsset(getAssets(), "VSVAB.TTF");
		  	
	        Bundle extras = getIntent().getExtras();
	        idBar = extras != null ? Long.parseLong(extras.getString("_id")) : null;
	        
	       
	        crearBBDDTapas();
	        populateFields();
	        getTapasBar();
	        
	        TextView comparte = (TextView) findViewById(R.id.compartir);
			comparte.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent share = new Intent(Intent.ACTION_SEND);
	        		 share.setType("image/jpeg"); // might be text, sound, whatever
	        		 FileOutputStream output;
	        		 File imagenTapa=null;
					try {
						output = openFileOutput("bar.jpeg",MODE_WORLD_READABLE );
					
						
		        		 InputStream inputPrueba = getResources().openRawResource(BarDetalle.this.getResources().getIdentifier(rutaImagen,
		            				"drawable", BarDetalle.this.getPackageName()));
		        		 byte[] buffer = new byte[128];
		        		 while ( ( inputPrueba.read(buffer) ) != -1  )
		        		 {
		        			 output.write(buffer);
		        			 output.flush();
		        		 }
		        	 
		        	 
		        		 imagenTapa = getFileStreamPath("bar.jpeg");
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
	        		 share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imagenTapa));
	        		 share.putExtra(Intent.EXTRA_TEXT, "Las tapas de " + nombreBar + " son increíbles. Pruébalas en el Festival de los Sentidos");
	        		 
	        		 startActivity(Intent.createChooser(share, "Compartir"));
					
				}
			});
			
	        setListAdapter(new FeriaAdapter(tapas));
	}
	
	public void getTapasBar()
	{
		miBBDDHelper.abrirBaseDatosFerias();
        tapas = miBBDDHelper.obtenerTapasBar(idBar);
	}
	  public void crearBBDDTapas() {
			miBBDDHelper = new BaseDatosFeriasHelper(this);
			try {
				miBBDDHelper.crearDataBaseFerias();
			} catch (IOException ioe) {
				throw new Error("Unable to create database");
			}
		}
	  
	  private void populateFields() {
	        if (idBar != null) {
	        	miBBDDHelper.abrirBaseDatosFerias();
	            bar = miBBDDHelper.fetchBar(idBar);
	            startManagingCursor(bar);
	             
	            TextView nombreText = (TextView) findViewById(R.id.detalle_bar);
	            //TextView descripcionText = (TextView) findViewById(R.id.bar_detalle_descripcion);
	            
	           
	            
	            
	            //descripcionText.setTypeface(font2);
	            nombreBar = bar.getString(
	            		bar.getColumnIndexOrThrow("NOMBRE"));
	            nombreText.setText(nombreBar);
	            nombreText.setTypeface(font2);
	            //descripcionText.setText(bar.getString(
	            //		bar.getColumnIndexOrThrow("DESCRIPCION")));
	            
	            
	            ImageView imagen = (ImageView)findViewById(R.id.imagen_bar);
	            rutaImagen = bar.getString(bar.getColumnIndexOrThrow("RUTA_LOGO"));
	            urlWeb = bar.getString(bar.getColumnIndexOrThrow("URL_WEB"));
	            imagen.setImageResource(
	            		this.getResources().getIdentifier(rutaImagen,
	            				"drawable", this.getPackageName()));
	            
	            TextView verWeb = (TextView)findViewById(R.id.ver_web);
	            
	            verWeb.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent = new Intent();
				        intent.setAction(Intent.ACTION_VIEW);
				        intent.addCategory(Intent.CATEGORY_BROWSABLE);
				        intent.setData(Uri.parse((urlWeb)));
				        startActivity(intent);
						
					}
				});
	        }
		   
	    }
	  
	  
	  class FeriaAdapter extends BaseAdapter {
			
			private List<Tapa>  tapas;
			 CheckBox probada;
			private LayoutInflater inflater=null;
			
			FeriaAdapter(List<Tapa> tapas) {
		    //super(context, c, from, to);
			inflater = (LayoutInflater)BarDetalle.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		    this.tapas=tapas;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
		    //View row = super.getView(position, convertView, parent);
			 View vi=convertView;
				 
				
			
			 vi = inflater.inflate(R.layout.tapas_row_detalle_bar, null);
		       
			 
		    ImageView icon = (ImageView) vi.findViewById(R.id.imagen);
		    
		    
		    TextView tapa = (TextView) vi.findViewById(R.id.tapa);
		    TextView descripcion = (TextView) vi.findViewById(R.id.descripcion);
		    
	    	Typeface font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
	    	Typeface font2 = Typeface.createFromAsset(getAssets(), "VSVAB.TTF");
	    	descripcion.setTypeface(font);
		    tapa.setTypeface(font2);
		    tapa.setText(tapas.get(position).getNombre());

		    
		    
		    vi.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					  int position = getListView().getPositionForView(v);
					  long id = getListView().getItemIdAtPosition(position);
					  Intent i = new Intent(getListView().getContext(), TapaDetalle.class);
				      i.putExtra("_id", id);
				      startActivityForResult(i, 1);
				}
			});
		    
		    icon.setImageResource(BarDetalle.this.getResources().getIdentifier(tapas.get(position).getRutaImagen(), "drawable", BarDetalle.this.getPackageName()));
		    probada = (CheckBox) vi.findViewById(R.id.probada);
		    RatingBar imagenVotada;
		    imagenVotada =(RatingBar) vi.findViewById(R.id.indicator_ratingbar);
		    
		    
		   
		    
		    return (vi);
		 }

		@Override
		public int getCount() {
			return tapas.size();
		}

		@Override
		public Object getItem(int position) {
				return tapas.get(position);
		}

		@Override
		public long getItemId(int position) {
			return tapas.get(position).getIdTapa();
		}


		}
	  

	  
	  
}
